package org.lex.swing.widgets;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.awt.geom.Rectangle2D;

/**
 * SplashScreen ����Ϊ: +----------------+ | | . . . LOGO . . . | |
 * |----------------| | Status Label | |----------------| | Progress |
 * +----------------+
 */
public class Splash {
	private static SplashScreen splash = null;
	private static Graphics2D g2 = null;
	static {
		splash = SplashScreen.getSplashScreen();
		if (null != splash) {
			g2 = splash.createGraphics();
			// ===========================
			Dimension size = splash.getSize();
			StatusLabel.initialize(size);
			ProgressBar.initialize(size);
			// initialize progressbar
			g2.setColor(ProgressBar.back);
			g2.fill(ProgressBar.bounds);
		}
	}

	public static void setStatus(String status) {
		if (isOpen()) {
			g2.setColor(Color.white);
			g2.fill(StatusLabel.bounds);
			g2.setBackground(Color.white);
			g2.setColor(Color.black);
			g2.drawString(status, StatusLabel.bounds.x, StatusLabel.bounds.y);
			advance();
			splash.update();
		}
	}

	private static Rectangle2D.Float block = new Rectangle2D.Float();

	private static void advance() {
		if (isOpen()) {
			g2.setColor(ProgressBar.fore);
			block.x = ProgressBar.value * ProgressBar.step;
			block.y = ProgressBar.bounds.y;
			block.width = ProgressBar.step;
			block.height = ProgressBar.height;
			g2.fill(block);
			ProgressBar.value++;
			// splash.update();
		}
	}

	private static boolean isOpen() {
		return null != SplashScreen.getSplashScreen();
	}

	// ProgressBar
	static class ProgressBar {
		// position & size
		static float step = 0;
		static int height = 3;
		static Rectangle2D.Float bounds = null;
		// color
		static Color back = Color.black;
		static Color fore = new Color(255, 142, 0);
		// value
		static int maximum = 4;
		static int value = 0;

		static void initialize(Dimension splashSize) {
			bounds = new Rectangle2D.Float(0, splashSize.height - height, splashSize.width, ProgressBar.height);
			step = (float) splashSize.width / ProgressBar.maximum;
		}
	}

	// StatusLabel
	static class StatusLabel {
		// insets
		static final int top = 3;
		static final int left = 0;
		static final int bottom = 5;
		static final int right = 0;
		// position & size
		static Rectangle2D.Float bounds = null;
		// color
		static Color back = Color.black;
		static Color fore = Color.white;

		static void initialize(Dimension splashSize) {
			int height = g2.getFontMetrics().getHeight();
			int width = splashSize.width;
			StatusLabel.bounds = new Rectangle2D.Float(left, splashSize.height - 3 * height + bottom, width, height);
		}
	}
}
